package com.pds.web.monitor.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pds.common.utils.DateUtils;
import com.pds.common.utils.SecurityUtils;
import com.pds.web.monitor.domain.PdsEnvironmentCheck;
import com.pds.web.monitor.mapper.PdsEnvironmentCheckMapper;
import com.pds.web.monitor.service.IPdsEnvironmentCheckService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class PdsEnvironmentCheckServiceImpl extends ServiceImpl<PdsEnvironmentCheckMapper, PdsEnvironmentCheck> implements IPdsEnvironmentCheckService {

    @Resource
    private PdsEnvironmentCheckMapper pdsEnvironmentCheckMapper;

    /**
     * 新增
     *
     * @param pdsEnvironmentCheck
     * @return
     */
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    @Override
    public PdsEnvironmentCheck addCheck(PdsEnvironmentCheck pdsEnvironmentCheck) {

//        Long userId = SecurityUtils.getLoginUser().getUser().getUserId();
//        Long deptId = SecurityUtils.getLoginUser().getUser().getDeptId();
        Long userId = 1L;
        pdsEnvironmentCheck.setUserId(userId);

        Date date = DateUtils.parseDate(DateUtils.getTime());

        if (pdsEnvironmentCheck.getCheckDate() == null) {
            pdsEnvironmentCheck.setCheckDate(date);
        }

        pdsEnvironmentCheck.setStatus("1");

        pdsEnvironmentCheck.setCreateTime(date);

        pdsEnvironmentCheckMapper.addCheck(pdsEnvironmentCheck);

        return pdsEnvironmentCheck;
    }

    /**
     * 删除 -- 根据id
     *
     * @param id
     * @return
     */
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    @Override
    public Boolean deleteCheck(Long id) {
        return pdsEnvironmentCheckMapper.deleteCheck(id) > 0;
    }

    /**
     * 查询 -- 分页/条件：userId，taskId，checkResult
     *
     * @param pdsEnvironmentCheck
     * @return
     */
    @Override
    public List<PdsEnvironmentCheck> selectCheckList(PdsEnvironmentCheck pdsEnvironmentCheck) {
        return pdsEnvironmentCheckMapper.selectCheckList(pdsEnvironmentCheck);
    }
}
